import Foundation
import Combine
import SwiftUI

// 连接类型
enum ConnectionType: String, CaseIterable, Identifiable {
    case bluetooth = "蓝牙"
    case wifi = "WiFi"
    case both = "蓝牙和WiFi"
    
    var id: String { self.rawValue }
}

// 应用程序设置
class AppSettings: ObservableObject {
    // 基本设置
    @Published var deviceName: String {
        didSet {
            UserDefaults.standard.set(deviceName, forKey: "deviceName")
        }
    }
    
    // 数据刷新频率（秒）
    @Published var dataRefreshRate: Double {
        didSet {
            UserDefaults.standard.set(dataRefreshRate, forKey: "dataRefreshRate")
        }
    }
    
    // 是否启用蓝牙连接
    @Published var isBluetoothEnabled: Bool {
        didSet {
            UserDefaults.standard.set(isBluetoothEnabled, forKey: "isBluetoothEnabled")
        }
    }
    
    // 是否自动连接
    @Published var isAutoConnectEnabled: Bool {
        didSet {
            UserDefaults.standard.set(isAutoConnectEnabled, forKey: "isAutoConnectEnabled")
        }
    }
    
    // 温度警告阈值
    @Published var temperatureWarningThreshold: Double {
        didSet {
            UserDefaults.standard.set(temperatureWarningThreshold, forKey: "temperatureWarningThreshold")
        }
    }
    
    // 湿度警告阈值
    @Published var humidityWarningThreshold: Double {
        didSet {
            UserDefaults.standard.set(humidityWarningThreshold, forKey: "humidityWarningThreshold")
        }
    }
    
    // 是否处于调试模式（不需要TCP连接也能进入应用）
    @Published var isDebugMode: Bool {
        didSet {
            UserDefaults.standard.set(isDebugMode, forKey: "isDebugMode")
        }
    }
    
    // 初始化
    init() {
        // 从UserDefaults加载设置，或使用默认值
        self.deviceName = UserDefaults.standard.string(forKey: "deviceName") ?? "智能小车"
        self.dataRefreshRate = UserDefaults.standard.double(forKey: "dataRefreshRate")
        self.isBluetoothEnabled = UserDefaults.standard.bool(forKey: "isBluetoothEnabled")
        self.isAutoConnectEnabled = UserDefaults.standard.bool(forKey: "isAutoConnectEnabled")
        self.temperatureWarningThreshold = UserDefaults.standard.double(forKey: "temperatureWarningThreshold")
        self.humidityWarningThreshold = UserDefaults.standard.double(forKey: "humidityWarningThreshold")
        self.isDebugMode = UserDefaults.standard.bool(forKey: "isDebugMode")
        
        // 如果dataRefreshRate未设置，使用默认值
        if self.dataRefreshRate == 0 {
            self.dataRefreshRate = 5.0
        }
        
        // 如果警告阈值未设置，使用默认值
        if self.temperatureWarningThreshold == 0 {
            self.temperatureWarningThreshold = 30.0 // 默认30°C
        }
        
        if self.humidityWarningThreshold == 0 {
            self.humidityWarningThreshold = 80.0 // 默认80%
        }
    }
    
    // 重置所有设置到默认值
    func resetToDefaults() {
        deviceName = "智能小车"
        dataRefreshRate = 5.0
        isBluetoothEnabled = false
        isAutoConnectEnabled = false
        temperatureWarningThreshold = 30.0
        humidityWarningThreshold = 80.0
        isDebugMode = false
    }
}

// 可用的摄像头分辨率
enum CameraResolution: String, CaseIterable, Identifiable {
    case res480p = "480p"
    case res720p = "720p"
    case res1080p = "1080p"
    
    var id: String { self.rawValue }
    
    var dimension: (width: Int, height: Int) {
        switch self {
        case .res480p:
            return (640, 480)
        case .res720p:
            return (1280, 720)
        case .res1080p:
            return (1920, 1080)
        }
    }
} 
